package com.dvt.faceScan;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.dvt.faceScan.httpUntil.XmlRpcUtils;
import com.dvt.faceScan.widget.Config;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;


public class Gzcx extends Activity implements View.OnClickListener{
    private TextView tv1;//item.xml里的TextView：Textviewname
    private TextView tv2;//item.xml里的TextView：Textviewage
    private TextView tv3;//item.xml里的TextView：Textviewage
    private TextView tv4;//item.xml里的TextView：Textviewage
    private ListView lv;//activity_main.xml里的ListView
    private BaseAdapter adapter;//要实现的类
    private Button btn_search;
    private EditText et_search;
    private String empName;
    private ArrayList<employeeModel> gzList = new ArrayList<employeeModel>();//实体类

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        empName="";
        requestEmployeeListCost();
        setContentView(R.layout.gzcx_activity);
        lv = (ListView) findViewById(R.id.listView1);
        btn_search = (Button) findViewById(R.id.Btn_serach2);
        et_search = (EditText) findViewById(R.id.Et_serach2);
        btn_search.setOnClickListener(this);

        adapter = new BaseAdapter() {
            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return gzList.size();//数目
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                LayoutInflater inflater = Gzcx.this.getLayoutInflater();
                View view;

                if (convertView == null) {
                    //因为getView()返回的对象，adapter会自动赋给ListView
                    view = inflater.inflate(R.layout.item, null);
                } else {
                    view = convertView;
                    Log.i("info", "有缓存，不需要重新生成" + position);
                }
                tv1 = (TextView) view.findViewById(R.id.Textviewdate);//找到Textviewname
                tv1.setText(gzList.get(position).getName());//设置参数

                tv2 = (TextView) view.findViewById(R.id.Textviewsalary);//找到Textviewage
                tv2.setText(gzList.get(position).getSalary() + "¥");//设置参数

                tv4 = (TextView) view.findViewById(R.id.Textviewcompany);//找到Textviewage
                tv4.setText(gzList.get(position).getCompany());//设置参数

                tv3 = (TextView) view.findViewById(R.id.Textviewgo);//找到Textviewage
                tv3.setText("");//设置参数
                return view;
            }

            @Override
            public long getItemId(int position) {//取在列表中与指定索引对应的行id
                return 0;
            }

            @Override
            public Object getItem(int position) {//获取数据集中与指定索引对应的数据项
                return null;
            }
        };
        lv.setAdapter(adapter);

        //获取当前ListView点击的行数，并且得到该数据
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                tv1 = (TextView) view.findViewById(R.id.Textviewdate);//找到Textviewname
                String str = tv1.getText().toString();//得到数据
            }
        });
    }

    //查询工资单
    private void requestEmployeeListCost(){
        Callable<String> c = new Callable<String>(){
            @Override
            public String call(){
                int uid = 1;
                String db = Config.DB;
                String uname = Config.account;
                String pwd = Config.password;
                String model = Config.hr_employee;
                String odooUrl = Config.baseURL;
                String url = String.format("%s/xmlrpc/2/object", odooUrl);
                int action_id = 121;

                Map<String,Object> params = new HashMap<String,Object>();
                params.put("action",action_id);
                Map<String,Object> context = new HashMap<String,Object>();
                context.put("lang", "zh_CN");
                context.put("tz", "Asia/Hong_Kong");
                context.put("uid", uid);
                context.put("params", params);

                List domain = asList(asList("name","ilike",empName));

                Map<String,Object> options = new HashMap<String,Object>();
                options.put("context", context);
                options.put("domain", domain);
                options.put("fields", asList("name","hired_company_id","salary"));
                options.put("limit", 80);
                Object requestObj = XmlRpcUtils.getXMLRPC(url, "execute_kw", asList(db, uid, pwd, model,"search_read",new ArrayList(),
                        options
                ));
                List r =asList((Object[])requestObj);
                for (Object object : r) {
                    Map<String,Object> a = (HashMap)object;
                    employeeModel employee = new employeeModel();
                    String astr = String.valueOf(a.get("name"));
                    if(!"false".equals(String.valueOf(a.get("hired_company_id")))) {
                        Object[] o = (Object[]) a.get("hired_company_id");
                        List<Object> ol = asList(o);
                        for (Object _o : ol) {
                            String str = String.valueOf(_o);
                            Log.d("1", str);
                            employee.setCompany(str);
                        }
                    }
                    String salary = String.valueOf(a.get("salary"));
                    if (astr.equals("false")) {
                        astr = "";
                    } else {
                        employee.setName(astr);
                    }

                    if (salary.equals("false")) {
                        salary = "";
                    } else {
                        employee.setSalary(salary);
                    }
                    gzList.add(employee);
                }
                return "";
            }
        };

        ExecutorService pool = Executors.newSingleThreadExecutor();
        Future<String> future = pool.submit(c);
        try{
            String result = future.get(5000, TimeUnit.SECONDS);
            Log.d("vvvvvv",result);
        }catch (Exception e){
            e.printStackTrace();
        }

    }
    private List asList(Object ...a){
        return Arrays.asList(a);
    }



    @Override
    public void onClick(View v) {
        if (btn_search == v) {
            empName = et_search.getText().toString();
            if(empName.length() == 0){
                empName = "";
            }
            gzList = new ArrayList<employeeModel>();//实体类
            requestEmployeeListCost();
            adapter.notifyDataSetChanged();
            lv.setAdapter(adapter);
        }
    }



}